﻿using System;
using DiplomaWeb.Properties;
using FluentValidation;
using FluentValidation.Results;

namespace DiplomaWeb.Models.Validators
{
    public class ChangePasswordViewModelValidator : AbstractValidator<ChangePasswordViewModel>
    {
        public ChangePasswordViewModelValidator()
        {
            RuleFor(r => r.OldPassword)
                .NotEmpty()
                .WithLocalizedMessage(() => ErrorMessages.msg_IsRequired);

            RuleFor(r => r.NewPasswordFirst)
                .NotEmpty()
                .WithLocalizedMessage(() => ErrorMessages.msg_IsRequired)
                .Length(6, 250)
                .WithLocalizedMessage(() => ErrorMessages.msg_ChangePasswordViewModel_IsShort);

            RuleFor(r => r.NewPasswordSecond)
                .NotEmpty()
                .WithLocalizedMessage(() => ErrorMessages.msg_IsRequired);
        }

        public override ValidationResult Validate(ValidationContext<ChangePasswordViewModel> context)
        {
            var result = base.Validate(context);

            if (result.Errors.Count == 0)
            {
                if (!String.Equals(context.InstanceToValidate.NewPasswordFirst,
                        context.InstanceToValidate.NewPasswordSecond,
                        StringComparison.CurrentCulture))
                {
                    result.Errors.Add(new ValidationFailure("NewPasswordFirst", ErrorMessages.msg_ChangePasswordViewModel_IsNotEquals));
                }
            }

            return result;
        }
    }
}